using System.Reflection;
using log4net;
using MediatR;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi.Models;
using Movies.API.Extensions;
using Movies.Application.Handlers;
using Movies.Core.Repositories;
using Movies.Core.Repositories.Base;
using Movies.Infrastructure.Data;
using Movies.Infrastructure.Repositories;
using Movies.Infrastructure.Repositories.Base;
using Movies.Jobs;
using Quartz;
using Quartz.AspNetCore;

namespace Movies.API
{
    public class Startup
    {
        public IConfiguration Configuration { get; }

        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public void ConfigureServices(IServiceCollection services)
        {

            // log4net
            services.AddLogging(configure => configure.AddLog4Net());
            services.AddControllers();
            services.AddApiVersioning();
            services.AddDbContext<MovieContext>(
                m => m.UseSqlServer(Configuration.GetConnectionString("MovieConnection")),
                ServiceLifetime.Singleton
            );
            services.AddEndpointsApiExplorer();
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo { Title = "Movie Review API", Version = "v1" });
            });
            services.AddAutoMapper(typeof(Startup));
            // System.Console.WriteLine("---------------------");
            // System.Console.WriteLine(typeof(CreateMovieCommandHandler).GetTypeInfo().Assembly.GetName());
            services.AddMediatR(typeof(CreateMovieCommandHandler).GetTypeInfo().Assembly);
            services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
            services.AddTransient<IMovieRepository, MovieRepository>();
            //
            // Add the required Quartz.NET services
            services.AddQuartz(q =>
            {
                q.AddJobAndTrigger<PickingAndShippingJob>(Configuration);
                // q.AddJobAndTrigger<ShippingInfoCollect>(builder.Configuration);
                // q.AddJobAndTrigger<ShippingSubmit>(builder.Configuration);
            });
            // ASP.NET Core hosting
            services.AddQuartzServer(options =>
            {
                // when shutting down we want jobs to complete gracefully
                options.WaitForJobsToComplete = true;
            });
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHttpsRedirection();
            app.UseRouting();
            app.UseAuthorization();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "Movie Review API V1");
            });

            // 将swagger重定向到根目录
            app.Use(
                async (context, next) =>
                {
                    if (context.Request.Path == "/")
                    {
                        ILog log = LogManager.GetLogger(typeof(Startup));
                        log.Info("访问swagger");
                        context.Response.Redirect("/swagger");
                        return;
                    }
                    await next();
                }
            );
        }
    }
}
